<template>
  <div class="mod-config">
    <!-- 操作表单 -->
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.keyword" placeholder="请输入标题" clearable @clear="getDataList()"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">搜索</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
<!--        <el-button-->
<!--          type="danger"-->
<!--          @click="deleteHandle()"-->
<!--          :disabled="dataListSelections.length <= 0"-->
<!--        >批量删除</el-button>-->
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
<!--      <el-table-column type="index" align="center" width="50" />-->
      <el-table-column align="center" type="index" width="80" label="序号" />
      <el-table-column prop="name" label="模型名称" align="center" />
      <el-table-column  label="描述" align="center">
        <template slot-scope="scope">
          <span>{{metaInfo.description}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="category" label="流程分类" align="center" />
      <el-table-column prop="key" label="key" align="center" />
      <el-table-column prop="revision" label="版本号" align="center" />
      <el-table-column prop="createTime" label="创建时间" align="center" />
      <el-table-column prop="lastUpdateTime" label="更新时间" align="center" />

      <el-table-column fixed="right" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button type="primary">
              功能
              <i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
<!--              <el-dropdown-item @click.native="addOrUpdateHandle(scope.row.id)">查看</el-dropdown-item>-->
              <el-dropdown-item @click.native="handleView(scope.row,scope.index)">模型图</el-dropdown-item>
              <el-dropdown-item @click.native="handleDeploy(scope.row.id)">部署</el-dropdown-item>
              <el-dropdown-item @click.native="deleteHandle(scope.row.id)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <model-add-or-update ref="modelAddOrUpdate" @refreshDataList="getDataList"></model-add-or-update>
    <model-delete ref="modelDetail"></model-delete>
  </div>
</template>

<script>
  import ModelDelete from './model-detail'
  import ModelAddOrUpdate from './model-add-or-update'
  export default {
    data() {
      return {
        dataForm: {
          keyword: ''
        },
        type: 2,
        dataList: [],
        metaInfo: {},
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: { ModelAddOrUpdate, ModelDelete },
    mounted() {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      async getDataList(isSearch) {
        this.dataListLoading = true
        var res = await this.$http.post('/act/model/page', {
          page: this.pageIndex,
          limit: this.pageSize
        })
        this.dataList = res.data.list
        this.dataList.forEach(x => {
          this.metaInfo = JSON.parse(x.metaInfo)
        })
        console.log(this.metaInfo)
        this.totalPage = res.data.totalCount
        this.dataListLoading = false
      },
      handleView(row) {
        let src = this.$http.adornUrl(
          `/modeler/modeler.html?modelId=${row.id}`
        )
        // let src = `http://192.168.10.81:12345/modeler/modeler.html?modelId=${row.id}`
        // this.$router.push({
        //   path: src,
        //   query: {
        //     src: src
        //   }
        // })
        this.$nextTick(() => this.$refs.modelDetail.initEditor(src))
      },
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle() {
        this.addOrUpdateVisible = true
        this.$nextTick(() => this.$refs.modelAddOrUpdate.initEditor())
      },
      // 部署
      handleDeploy(id) {
        this.$confirm(`确定要进行部署操作吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/act/model/deploy/${id}`),
            method: 'post'
          }).then(data => {
            if (data && data.code === 200) {
              this.$notify.success({
                title: '提示',
                message: '操作成功'
              })
              this.dataForm.page > 1 && this.dataList.length <= 1 && this.dataForm.page--
              this.getDataList()
            } else {
              this.$notify.warning({
                title: '提示',
                message: data.msg
              })
            }
          })
        })
      },
      // 删除
      async deleteHandle(id) {
        var confirm = await this.$confirm(`确定要进行删除操作吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        if (confirm !== 'confirm') return

        let res = await this.$http({
          url: this.$http.adornUrl(`/act/model/del/${id}`),
          method: 'post'
        })
        if (res && res.code === 200) {
          this.$notify.success({ title: '成功', message: '成功' })
          this.pageIndex > 1 && this.dataList.length <= 1 && this.pageIndex --
          this.getDataList()
        } else {
          this.$notify.error({ title: '错误', message: res.msg })
        }
      }
    }
  }
</script>

<style scoped>
</style>
